package com.ruoyi.bizsys.service.impl;

import java.util.List;
import java.util.Arrays;
import java.util.Optional;

import com.ruoyi.bizsys.domain.LoanIou;
import com.ruoyi.bizsys.vo.LoanIouDeferredVo;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bizsys.mapper.LoanIouDeferredMapper;
import com.ruoyi.bizsys.domain.LoanIouDeferred;
import com.ruoyi.bizsys.service.ILoanIouDeferredService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


/**
 * 延期借据Service业务层处理
 *
 * @author wqg
 * @date 2022-06-09
 */
@Service
public class LoanIouDeferredServiceImpl extends ServiceImpl<LoanIouDeferredMapper, LoanIouDeferred> implements ILoanIouDeferredService {

    @Autowired
    LoanIouDeferredMapper loanIouDeferredMapper;
    /**
     * 查询延期借据
     *
     * @param id 延期借据ID
     * @return 延期借据
     */
    @Override
    public LoanIouDeferred selectLoanIouDeferredById(Long id) {
        return this.getById(id);
    }

    /**
     * 查询延期借据列表
     *
     * @param loanIouDeferred 延期借据
     * @return 延期借据
     */
    @Override
    public List<LoanIouDeferredVo> selectLoanIouDeferredList(LoanIouDeferred loanIouDeferred) {
        return loanIouDeferredMapper.selectLoanIouDeferredList(loanIouDeferred);
    }

    /**
     * 新增延期借据
     *
     * @param loanIouDeferred 延期借据
     * @return 结果
     */
    @Override
    public int insertLoanIouDeferred(LoanIouDeferred loanIouDeferred) {
        loanIouDeferred.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insert(loanIouDeferred);
    }

    /**
     * 修改延期借据
     *
     * @param loanIouDeferred 延期借据
     * @return 结果
     */
    @Override
    public int updateLoanIouDeferred(LoanIouDeferred loanIouDeferred) {
        loanIouDeferred.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(loanIouDeferred);
    }

    /**
     * 批量删除延期借据
     *
     * @param ids 需要删除的延期借据ID
     * @return 结果
     */
    @Override
    public int deleteLoanIouDeferredByIds(Long[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除延期借据信息
     *
     * @param id 延期借据ID
     * @return 结果
     */
    @Override
    public int deleteLoanIouDeferredById(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public void updateByloanSerialNo(LoanIou loanIou) {
        if (null!=loanIou.getIsDeferred() && loanIou.getIsDeferred() == 1) {
            LoanIouDeferred loanIouDeferred = new LoanIouDeferred();
            loanIouDeferred.setStatus(loanIou.getIouStatus());
            loanIouDeferred.setLoanSerialNo(loanIou.getLoanSerialNo());
            baseMapper.updateByloanSerialNo(loanIouDeferred);
        }
    }

    @Override
    public Integer getCurrentDeferredCnt(String loanSerialNo) {
        return baseMapper.selectList(new QueryWrapper<LoanIouDeferred>().eq("loan_serial_no", loanSerialNo).eq("state", 1)).size();
    }

    @Override
    public String getJieQingLiXi(String customerNo) {
        return baseMapper.getJieQingLiXi(customerNo);
    }

    @Override
    public String getJieQingFuWuFei(String customerNo) {
        return baseMapper.getJieQingFuWuFei(customerNo);
    }

    @Override
    public String getcurrentinterest(String loanSerialNo) {
        return baseMapper.getcurrentinterest(loanSerialNo);
    }

    @Override
    public String getloanIouDeferredService(String loanSerialNo) {
        return baseMapper.getloanIouDeferredService(loanSerialNo);
    }

    @Override
    public LoanIouDeferred getDeferred(Long loanSerialDeferredNo) {
        QueryWrapper<LoanIouDeferred> queryWrapper = new QueryWrapper<LoanIouDeferred>().eq("loan_serial_no", loanSerialDeferredNo);
        return getOne(queryWrapper);
    }

    @Override
    public int getDeferredTotalDay(String custNo) {
        return Optional.ofNullable(baseMapper.getDeferredTotalDay(custNo)).orElse(0);
    }

}
